package com.youyi.yycutsdklibrary.CropCore;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.yanzhenjie.permission.runtime.Permission;
import com.youyi.yycutsdklibrary.CropCore.CropImage;
import com.youyi.yycutsdklibrary.CropCore.CropImageView;
import com.youyi.yycutsdklibrary.CropCore.CropOverlayView;
import com.lkx.liandianqinew.R;
import java.io.File;
import java.io.IOException;

public class CropImageActivity extends AppCompatActivity implements CropImageView.OnSetImageUriCompleteListener, CropImageView.OnCropImageCompleteListener {
    private LinearLayout mBottom;
    private Uri mCropImageUri;
    private CropImageView mCropImageView;
    private CropImageOptions mOptions;

    @Override // android.support.v4.app.SupportActivity, android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        requestWindowFeature(1);
        getWindow().setFlags(1024, 1024);
        setContentView(R.layout.crop_image_activity);
        this.mBottom = (LinearLayout) findViewById(R.id._bottom_view);
        this.mCropImageView = (CropImageView) findViewById(R.id.cropImageView);
        this.mCropImageView.setOnPressListener(new CropOverlayView.OnPressListener() {
            /* class com.youyi.yycutsdklibrary.CropCore.CropImageActivity.AnonymousClass1 */

            @Override // com.youyi.yycutsdklibrary.CropCore.CropOverlayView.OnPressListener
            public void isPress(boolean z) {
                if (z) {
                    CropImageActivity.this.mBottom.setVisibility(View.GONE);
                } else {
                    CropImageActivity.this.mBottom.setVisibility(View.VISIBLE);
                }
            }
        });
        ((TextView) findViewById(R.id.btn_cancel)).setOnClickListener(new View.OnClickListener() {
            /* class com.youyi.yycutsdklibrary.CropCore.CropImageActivity.AnonymousClass2 */

            public void onClick(View view) {
                CropImageActivity.this.setResultCancel();
            }
        });
        ((TextView) findViewById(R.id.btn_rotate)).setOnClickListener(new View.OnClickListener() {
            /* class com.youyi.yycutsdklibrary.CropCore.CropImageActivity.AnonymousClass3 */

            public void onClick(View view) {
                CropImageActivity cropImageActivity = CropImageActivity.this;
                cropImageActivity.rotateImage(cropImageActivity.mOptions.rotationDegrees);
            }
        });
        ((TextView) findViewById(R.id.btn_done)).setOnClickListener(new View.OnClickListener() {
            /* class com.youyi.yycutsdklibrary.CropCore.CropImageActivity.AnonymousClass4 */

            public void onClick(View view) {
                CropImageActivity.this.cropImage();
            }
        });
        Bundle bundleExtra = getIntent().getBundleExtra(CropImage.CROP_IMAGE_EXTRA_BUNDLE);
        this.mCropImageUri = (Uri) bundleExtra.getParcelable(CropImage.CROP_IMAGE_EXTRA_SOURCE);
        this.mOptions = (CropImageOptions) bundleExtra.getParcelable(CropImage.CROP_IMAGE_EXTRA_OPTIONS);
        if (bundle == null) {
            Uri uri = this.mCropImageUri;
            if (uri == null || uri.equals(Uri.EMPTY)) {
                if (CropImage.isExplicitCameraPermissionRequired(this)) {
                    requestPermissions(new String[]{Permission.CAMERA}, 2011);
                } else {
                    CropImage.startPickImageActivity(this);
                }
            } else if (CropImage.isReadExternalStoragePermissionsRequired(this, this.mCropImageUri)) {
                requestPermissions(new String[]{Permission.READ_EXTERNAL_STORAGE}, 201);
            } else {
                this.mCropImageView.setImageUriAsync(this.mCropImageUri);
            }
        }
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity
    public void onStart() {
        super.onStart();
        this.mCropImageView.setOnSetImageUriCompleteListener(this);
        this.mCropImageView.setOnCropImageCompleteListener(this);
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity
    public void onStop() {
        super.onStop();
        this.mCropImageView.setOnSetImageUriCompleteListener(null);
        this.mCropImageView.setOnCropImageCompleteListener(null);
    }

    @Override // android.support.v4.app.FragmentActivity
    public void onBackPressed() {
        super.onBackPressed();
        setResultCancel();
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v4.app.FragmentActivity
    public void onActivityResult(int i, int i2, Intent intent) {
        if (i == 200) {
            if (i2 == 0) {
                setResultCancel();
            }
            if (i2 == -1) {
                Uri pickImageResultUri = CropImage.getPickImageResultUri(this, intent);
                this.mCropImageUri = pickImageResultUri;
                if (CropImage.isReadExternalStoragePermissionsRequired(this, pickImageResultUri)) {
                    requestPermissions(new String[]{Permission.READ_EXTERNAL_STORAGE}, 201);
                    return;
                }
                Log.d("CropImageActivity", "mCropImageUri:" + this.mCropImageUri);
                this.mCropImageView.setImageUriAsync(this.mCropImageUri);
            }
        }
    }

    @Override // android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback, android.support.v4.app.FragmentActivity
    public void onRequestPermissionsResult(int i, String[] strArr, int[] iArr) {
        if (i == 201) {
            Uri uri = this.mCropImageUri;
            if (uri == null || iArr.length <= 0 || iArr[0] != 0) {
                Toast.makeText(this, R.string.crop_image_activity_no_permissions, 1).show();
                setResultCancel();
            } else {
                this.mCropImageView.setImageUriAsync(uri);
            }
        }
        if (i == 2011) {
            CropImage.startPickImageActivity(this);
        }
    }

    @Override // com.youyi.yycutsdklibrary.CropCore.CropImageView.OnSetImageUriCompleteListener
    public void onSetImageUriComplete(CropImageView cropImageView, Uri uri, Exception exc) {
        if (exc == null) {
            if (this.mOptions.initialCropWindowRectangle != null) {
                this.mCropImageView.setCropRect(this.mOptions.initialCropWindowRectangle);
            }
            if (this.mOptions.initialRotation > -1) {
                this.mCropImageView.setRotatedDegrees(this.mOptions.initialRotation);
                return;
            }
            return;
        }
        setResult(null, exc, 1);
    }

    @Override // com.youyi.yycutsdklibrary.CropCore.CropImageView.OnCropImageCompleteListener
    public void onCropImageComplete(CropImageView cropImageView, CropImageView.CropResult cropResult) {
        setResult(cropResult.getUri(), cropResult.getError(), cropResult.getSampleSize());
    }

    /* access modifiers changed from: protected */
    public void cropImage() {
        if (this.mOptions.noOutputImage) {
            setResult(null, null, 1);
            return;
        }
        this.mCropImageView.saveCroppedImageAsync(getOutputUri(), this.mOptions.outputCompressFormat, this.mOptions.outputCompressQuality, this.mOptions.outputRequestWidth, this.mOptions.outputRequestHeight, this.mOptions.outputRequestSizeOptions);
    }

    /* access modifiers changed from: protected */
    public void rotateImage(int i) {
        this.mCropImageView.rotateImage(i);
    }

    /* access modifiers changed from: protected */
    public Uri getOutputUri() {
        Uri uri = this.mOptions.outputUri;
        if (uri != null && !uri.equals(Uri.EMPTY)) {
            return uri;
        }
        try {
            return Uri.fromFile(File.createTempFile("cropped", this.mOptions.outputCompressFormat == Bitmap.CompressFormat.JPEG ? ".jpg" : this.mOptions.outputCompressFormat == Bitmap.CompressFormat.PNG ? ".png" : ".webp", getCacheDir()));
        } catch (IOException e) {
            throw new RuntimeException("Failed to create temp file for output image", e);
        }
    }

    /* access modifiers changed from: protected */
    public void setResult(Uri uri, Exception exc, int i) {
        setResult(exc == null ? -1 : 204, getResultIntent(uri, exc, i));
        finish();
    }

    /* access modifiers changed from: protected */
    public void setResultCancel() {
        setResult(0);
        finish();
    }

    /* access modifiers changed from: protected */
    public Intent getResultIntent(Uri uri, Exception exc, int i) {
        CropImage.ActivityResult activityResult = new CropImage.ActivityResult(this.mCropImageView.getImageUri(), uri, exc, this.mCropImageView.getCropPoints(), this.mCropImageView.getCropRect(), this.mCropImageView.getRotatedDegrees(), this.mCropImageView.getWholeImageRect(), i);
        Intent intent = new Intent();
        intent.putExtras(getIntent());
        intent.putExtra(CropImage.CROP_IMAGE_EXTRA_RESULT, activityResult);
        return intent;
    }
}
